<template>
  <div class='home-category'>
    <ul class="menu">
      <li v-for="item in list" :key="item.id" @mouseenter="currentCateId = item.id">
        <!-- 一级分类 -->
        <RouterLink to="/">{{ item.name }}</RouterLink>
        <!-- 二级分类 -->
        <!-- 使用骨架屏组件进行网络慢时的优化 -->
        <template v-if="item.children">
          <RouterLink v-for="item1 in item.children" :key="item1.id" to="/">{{ item1.name }}</RouterLink>
        </template>
        <span v-else>
          <xtx-skeleton width="60px" :animated="true" height="18px" style="margin-right:5px" bg="rgba(255,255,255,0.2)" />
          <xtx-skeleton width="60px" animated height="18px" style="margin-right:5px" bg="rgba(255,255,255,0.2)" />
        </span>
        <!-- 分类推荐的商品弹层 -->
        <div class="layer">
          <h4>{{ item.id === 'brand' ? '品牌' : '分类' }}推荐 <small>根据您的购买或浏览记录推荐</small></h4>
          <ul v-if="currentCate && currentCate.goods">
            <!-- 分类推荐 -->
            <template v-if="item.id !== 'brand'">
              <li v-for="goods in currentCate.goods" :key="goods.id">
                <RouterLink to="/">
                  <img :src="goods.picture" alt="">
                  <div class="info">
                    <p class="name ellipsis-2">{{ goods.name }}</p>
                    <p class="desc ellipsis">{{ goods.desc }}</p>
                    <p class="price"><i>¥</i>{{ goods.price }}</p>
                  </div>
                </RouterLink>
              </li>
            </template>
            <!-- 品牌推荐 -->
            <template v-else>
              <li class="brand" v-for="goods in currentCate.goods" :key="goods.id">
                <RouterLink to="/">
                  <img :src="goods.picture" alt="">
                  <div class="info">
                    <p class="place"><i class="iconfont icon-dingwei"></i>{{ goods.place }}</p>
                    <p class="name ellipsis">{{ goods.name }}</p>
                    <p class="desc ellipsis-2">{{ goods.desc }}</p>
                  </div>
                </RouterLink>
              </li>
            </template>
          </ul>
        </div>
      </li>
    </ul>
  </div>
</template>

<script setup>
import { computed, reactive, ref } from 'vue'
import { useStore } from 'vuex'
import { homeBrandsApi } from '@/api/home'
//  定义分类数据中额外的品牌数据，数据格式要跟分类数据保持一致
const brand = reactive({
  id: 'brand',
  name: '品牌',
  goods: [],
  children: [{
    id: 'brand-children',
    name: '品牌推荐'
  }]
})

// 获取品牌数据
homeBrandsApi(6).then(({ result }) => {
  brand.goods = result
})

// 从vuex中获取分类数据
const store = useStore()
const list = computed(() => {
  const cateData = store.state.cate.cateList.map(item => {
    // 处理数据
    return {
      id: item.id,
      name: item.name,
      children: item.children && item.children.slice(0, 2),
      goods: item.goods
    }
  })
  // 把品牌数据放入到数组的最后
  cateData.push(brand)
  return cateData
})
// 获取某个分类id对应的数据
const currentCateId = ref(null)
const currentCate = computed(() => {
  return list.value.find(item => item.id === currentCateId.value)
})
</script>
<style lang="less" scoped>
.home-category {
  width: 250px;
  height: 500px;
  background: rgba(0, 0, 0, 0.8);
  position: relative;
  z-index: 99;

  .menu {
    >li {
      padding-left: 40px;
      height: 50px;
      line-height: 50px;

      &:hover {
        background: @xtxColor;

        // 鼠标悬浮在对应的li上面显示对应的推荐商品
        .layer {
          display: block;
        }
      }

      >a {
        margin-right: 4px;
        color: #fff;

        &:first-child {
          font-size: 16px;
        }
      }

      // 推荐商品弹层
      .layer {
        width: 990px;
        height: 500px;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        left: 250px;
        top: 0;
        display: none;
        padding: 0 15px;

        h4 {
          font-size: 20px;
          font-weight: normal;
          line-height: 80px;

          small {
            font-size: 16px;
            color: #666;
          }
        }

        ul {
          display: flex;
          flex-wrap: wrap;

          li {
            width: 310px;
            height: 120px;
            margin-right: 15px;
            margin-bottom: 15px;
            border: 1px solid #eee;
            border-radius: 4px;
            background: #fff;

            // 品牌的样式
            &.brand {
              height: 180px;

              a {
                align-items: flex-start;

                img {
                  width: 120px;
                  height: 160px;
                }

                .info {
                  p {
                    margin-top: 8px;
                  }

                  .place {
                    color: #999;
                  }
                }
              }
            }

            &:nth-child(3n) {
              margin-right: 0;
            }

            a {
              display: flex;
              width: 100%;
              height: 100%;
              align-items: center;
              padding: 10px;

              &:hover {
                background: #e3f9f4;
              }

              img {
                width: 95px;
                height: 95px;
              }

              .info {
                padding-left: 10px;
                line-height: 24px;
                width: 190px;

                .name {
                  font-size: 16px;
                  color: #666;
                }

                .desc {
                  color: #999;
                }

                .price {
                  font-size: 22px;
                  color: @priceColor;

                  i {
                    font-size: 16px;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}
</style>